package com.le.tester.leetcode;

import com.le.tester.leetcode.common.TreeNode;

import java.util.Objects;

/**
 * currentTime:2022/3/9 10:11
 * description:最小的深度（最多有两个节点的有序树）
 */
public class MinDepth {

    public int minDepth(TreeNode root) {
        if (Objects.isNull(root)) {
            return 0;
        }

        if (Objects.isNull(root.left) && Objects.isNull(root.right)) {
            return 1;
        }

        int result = Integer.MAX_VALUE;
        if (Objects.nonNull(root.left)) {
            result = Math.min(this.minDepth(root.left), result);
        }

        if (Objects.nonNull(root.right)) {
            result = Math.min(this.minDepth(root.right), result);
        }

        return result + 1;
    }

    public static void main(String[] args) {
        MinDepth minDepth = new MinDepth();
        TreeNode secondLeftTree = new TreeNode(15);
        TreeNode secondRightNode = new TreeNode(7);
        TreeNode leftTree = new TreeNode(9);
        TreeNode rightTree = new TreeNode(20, secondLeftTree, secondRightNode);

        TreeNode treeNode = new TreeNode(3, leftTree, rightTree);
        System.out.println(minDepth.minDepth(treeNode));
    }
}
